package com.javabase.y2022.m05.day0516.v03;

/**
 * @descriptions: Solution
 * @author: Tom
 * @date: 2022/5/16 下午 01:10
 * @version: 1.0
 */
public class Solution {
    // 使用数组实现不超过固定大小的隊列
    public static class MyQueue02{
        int size;
        int length;
        int pollIndex;
        int pushIndex;
        int MAX_SIZE;
        int[] arr;
        public MyQueue02(int length){
            if(length > MAX_SIZE){
                throw new RuntimeException("length is too big~");
            }
            arr = new int[length];
        }
        public int pop(){
            if(size == 0 ){
                throw new RuntimeException("size is zero~");
            }
            int ans = arr[this.pollIndex];
            this.pollIndex = nextIndex(this.pollIndex);
            this.size--;
            return ans;
        }
        public void push(int value){
            if(size == length){
                throw new RuntimeException("size is full!");
            }
            arr[this.pushIndex] = value;
            this.pushIndex = nextIndex(this.pushIndex);
        }

        public int nextIndex(int index){
            return index + 1 == this.length? 0:index+1;
        }
    }

}
